<template>
  <Timeline :class="[styles.module]" v-bind="{ ...attrs, ...props }"  v-if="timeLineList && timeLineList.length >= 1">
    <TimelineEntry
      v-for="(item, index) of timeLineList"
      :key="index"
      v-bind="item"
    >
      <template v-slot:subtitle>
        <div class="timelinetitle">
          <div class="subtitle">{{ item.subtitle }}</div>
          <div class="line"></div>
          <Avatar size="24px" class="titleavator">
            <img :src="item.avatarTime" />
          </Avatar>
          <div style="margin-left: 10px">{{ item.name }}</div>
        </div>
      </template>
    </TimelineEntry>
    <TimelineEntry
    title="Event Title"
    subtitle="February 22, 1986"
  >
    <div>
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </div>
  </TimelineEntry>
  </Timeline>
</template>

<script setup>
  import TimelineEntry from './TimelineEntry.jsx';
  import Timeline from '@teld/q-components/Timeline';
  import Avatar from '@teld/q-components/Avatar';
  import useMappingProps from 't/common/hooks/useMappingProps';
  import styles from './index.module.scss';
  import './index.scss';
  import {
    commonProps,
    fixNameSpace,
    pickOwnForBasicComponent,
  } from 't/common/utils';
  import {
    defineComponent,
    onMounted,
    ref,
    useSlots,
    useAttrs,
    computed,
  } from 'vue';

  const slots = useSlots();
  const attrs = useAttrs();
  const props = defineProps(
    {
      ...commonProps,
      ...Timeline.props,
      //数据源类型
      itemsDataSourceType: {
        type: String,
        default() {
          return 'static'; // static, dynamic
        },
      },
      //静态数据源
      staticItems: {
        type: Array,
        default() {
          return [];
        },
      },
      //动态数据源
      listDataSource: {
        type: Array,
        default() {
          return [];
        },
      },
      //启用倒序
      reverse: {
        type: Boolean,
        default() {
          return false;
        },
      },
      //组件整体颜色
      componentColor: {
        type: String,
        default() {
          return ''; // 暂时的颜色，规范未定
        },
      },
      titleField: {
        type: String,
        default() {
          return '';
        },
      },
      bodyField: {
        type: String,
        default() {
          return '';
        },
      },
      subtitleField: {
        type: String,
        default() {
          return '';
        },
      },
      colorField: {
        type: String,
        default() {
          return '';
        },
      },
      iconField: {
        type: String,
        default() {
          return '';
        },
      },
      //辅助图片
      avatarTimeField: {
        type: String,
        default() {
          return '';
        },
      },
      //辅助文本
      avatanameField: {
        type: String,
        default() {
          return '用户';
        },
      },
      //节点上的文本
      textField: {
        type: String,
        default() {
          return '新建';
        },
      },
    },
  );
  const [mappingProps, mappingExpose] = useMappingProps(props);
  defineExpose(mappingExpose);
  const timeLineList = computed(() => {
    const timeLineList =
      mappingProps.itemsDataSourceType === 'static'
        ? formatTimeLineList(mappingProps.staticItems)
        : formatTimeLineList(mappingProps.listDataSource);
    return timeLineList;
  });
  const formatTimeLineList = list => {
    if (!(list instanceof Array)) {
      return [];
    }
    return list.map(item => {
      const obj = {
        title: item[mappingProps.titleField] ?? item.title,
        body: item[mappingProps.bodyField] ?? item.body,
        subtitle: item[mappingProps.subtitleField] ?? item.subtitle,
        color: item[mappingProps.colorField] ?? item.color,
        icon: item[mappingProps.iconField] ?? item.icon,
        avatar: item[mappingProps.avatarTimeField] ?? item.avatar,
        avataname: item[mappingProps.avatanameField] ?? item.avataname,
        text: item[mappingProps.textField] ?? item.text,
      };
      return { ...item, ...obj };
    });
  };
</script>

<style lang="scss" scoped>
  .timelinetitle {
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    display: flex;
  }
  .subtitle {
    font-size: 16px;
  }
  .line {
    background: #9b9b9b; /*背景色为浅灰色*/
    width: 1px; /*设置宽高*/
    height: 16px;
    margin-left: 20px;
  }
  .titleavator {
    margin-left: 10px;
  }
</style>
